import Toast from '@vant/weapp/toast/toast';
Page({
  data: {
    userInfo: {},
    inputContent: '',// 输入内容
    fileList: [], // 上传的文件
    type: 0,//选择的类型
  },
  onLoad: function() {
    var info = wx.getStorageSync("userinfo");
    this.setData({
        userInfo: info,
    })
  },
  
  // 处理选择变化  
  radioChange: function(e) {  
    this.setData({  
      type: e.detail.value  
    });
  },

  inputContentChange(e) {
    this.setData({
      inputContent: e.detail.value//e.detail.value表示输入框的当前值
    })
  },

  afterRead(event) {
    const self = this;
    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: 'http://47.120.31.85:8080/word/upload',
      filePath: file.url,
      name: 'file',
      success(res) {
        const obj = JSON.parse(res.data)
        // 上传完成需要更新 fileList
        const { fileList = [] } = self.data;
        fileList.push({ ...file, url: obj['data'] });
        self.setData({ fileList });
      },
    });
  },

  publish() {
    const self = this;
    let urls = []
    for ( let i = 0 ; i < self.data.fileList.length ; i ++ ) {
      urls.push(self.data.fileList[i].url)
    }
    const uploadFormData = {
      content: self.data.inputContent,
      files: urls,
      type: self.data.type,
    }
    wx.request({
      url: 'http://47.120.31.85:8080/word/publish?username=' + self.data.userInfo.nickName,
      method: 'Post',
      data: uploadFormData,
      success: function(res) {
        Toast.success('上传成功!');
        setTimeout(()=>{
          wx.navigateBack({
            delta: 1
          })
        }, 3000)
      }
    })
  },
})